In [2]:
x=c(2,7,5)
In [3]:
x
Out[3]:
In [4]:
seq(from=4, length=3, by=3)
Out[4]:
In [5]:
y=seq(from=4, length=3, by=3)
In [7]:
x+y
Out[7]:
In [8]:
x*y
Out[8]:
In [9]:
x^y
Out[9]:
In [10]:
x[2] # second element of x
Out[10]:
In [12]:
x[2:3] # all elements from the second, up to and including the third element
Out[12]:
In [15]:
x[-2] # all elements, except for the second one
Out[15]:
In [17]:
x[-c(1,2)] # all elements, except for the first and second one
Out[17]:
In [21]:
z = matrix(seq(1,12), 4, 3) # matrix with 4 rows and 3 columns
In [22]:
z
Out[22]:
In [23]:
z[3:4, 2:3] # show rows 3 to 4, and cols 2 to 3
Out[23]:
In [25]:
z[,2:3] # show all rows, but only cols 2 to 3
Out[25]:
In [31]:
z[,1] # first col only, by default converted into a vector
Out[31]:
In [33]:
z[,1, drop=FALSE] # first col only, keep it as a matrix
Out[33]:
In [34]:
dim(z)
Out[34]:
In [35]:
ls() # known variables in your namespace(s)
Out[35]:
In [36]:
rm(y) # remove a variable
In [37]:
ls()
Out[37]:
In [40]:
x=runif(50) # 50 random uniform numbers between 0 and 1
In [42]:
y=rnorm(50) # 50 random normal vars between 0 and 1
In [44]:
plot(x,y)
In [48]:
# change plotting character / color; add axis labels
plot(x,y,xlab='Random Uniform', ylab='Random Normal', pch='*', col='blue')
In [52]:
# par() sets global plotting params
# mfrow: plot with 2 rows and 1 col
par(mfrow = c(2,1))
In [53]:
plot(x,y)
hist(y)
In [54]:
# reset plot command
par(mfrow = c(1,1))
In [55]:
ls()
Out[55]:
In [58]:
Auto=read.csv('data/Auto.csv')
In [60]:
head(Auto)
Out[60]:
In [61]:
names(Auto)
Out[61]:
In [62]:
dim(Auto)
Out[62]:
In [63]:
class(Auto)
Out[63]:
In [64]:
summary(Auto)
Out[64]:
In [67]:
plot(Auto$cylinders, Auto$mpg)
In [69]:
# attributes can be a abbreviated
# plot(Auto$cyl, Auto$mpg)
In [73]:
attach(Auto) # put column header names into current namespace
In [74]:
search()
Out[74]:
In [96]:
par(mfrow = c(2,2))
plot(cylinders)
hist(cylinders)
plot(as.factor(cylinders), main="Factors of cylinders")
plot(as.factor(cylinders), mpg, main="Boxplot: cyl. vs. mpg",
xlab="cylinders", ylab="mpg")